package com.hyj.ecp.backstage.sys.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hyj.ecp.backstage.sys.bean.qo.LoginQO;
import com.hyj.ecp.backstage.sys.bean.vo.User;
import com.hyj.ecp.backstage.sys.service.IRoleService;
import com.hyj.ecp.common.constant.SysConstant;

@Controller
@SessionAttributes("user")
@RequestMapping("/backstage/sys/role/*")
public class RoleManageAction {
	Log log = LogFactory.getLog(RoleManageAction.class);
	@Autowired
    private IRoleService service;
	
	@RequestMapping(value="toUserManager.do")
    public String toUserManager()
    {
		log.info("toUserManager start..");
	    return "view/backstage/sys/userManager";
	 
    }
	
	@ResponseBody
	@RequestMapping(value="queryUserManager.do")
    public Object queryUserManager(LoginQO qo,ModelMap map)
    {
		log.info("queryUserManager.do start..");
		Map<String,Object> m = service.queryBackUser(qo);
	    return m;
    }
	
	
	@RequestMapping(value="toAddUser.do")
    public String toAddUser(LoginQO qo,ModelMap map)
    {
		log.info("toAddUser.do start..");
		List<Map<String,Object>> rols = service.queryRole(qo);
		map.put("rols", rols);
	    return "view/backstage/sys/addUser";
    }
	
	
	@ResponseBody
	@RequestMapping(value="saveUser.do")
    public Object saveUser(LoginQO qo,ModelMap map)
    {
		log.info("saveUser.do start..");
		User u =  (User)map.get("user");
        Map<String,Object> vo = new HashMap<String,Object>();
        try{
        qo.setOptUser(u.getAccount());
    	int a = service.saveUser(qo);
    	if(a>0){
    		vo.put("returnCode", 0);
    		vo.put("saveNum", a);
    		vo.put("msg", SysConstant.SAVE_SUCCESS_STR);
    	}else{
    		vo.put("returnCode", 999);
    		vo.put("saveNum", a);
    		vo.put("msg", SysConstant.SAVE_FAIL_STR);
    	}
        }catch(Exception e){
        	log.error("addtypePage.do",e);
        	vo.put("returnCode", 999);
    		vo.put("saveNum", 0);
    		vo.put("msg", SysConstant.SAVE_FAIL_STR);
        }
    	return vo;
    }
	
	
	@RequestMapping(value="toUpdateUser.do")
    public String toUpdateUser(LoginQO qo,ModelMap map)
    {
		log.info("toUpdateUser.do start..");
		Map<String,Object> vo = service.queryBackSingleUser(qo);
		List<Map<String,Object>> rols = service.queryRole(qo);
		map.put("vo", vo);
		map.put("rols", rols);
		
	    return "view/backstage/sys/updateUser";
    }
	
	
	@ResponseBody
	@RequestMapping(value="updateUser.do")
    public Object updateUser(LoginQO qo,ModelMap map)
    {
		log.info("updateUser.do start..");
		User u =  (User)map.get("user");
        Map<String,Object> vo = new HashMap<String,Object>();
        try{
        qo.setOptUser(u.getAccount());
    	int a = service.updateUser(qo);
    	if(a>0){
    		vo.put("returnCode", 0);
    		vo.put("saveNum", a);
    		vo.put("msg", SysConstant.UPDATE_SUCCESS_STR);
    	}else{
    		vo.put("returnCode", 999);
    		vo.put("saveNum", a);
    		vo.put("msg", SysConstant.UPDATE_FAIL_STR);
    	}
        }catch(Exception e){
        	log.error("updateUser.do",e);
        	vo.put("returnCode", 999);
    		vo.put("saveNum", 0);
    		vo.put("msg", SysConstant.UPDATE_FAIL_STR);
        }
    	return vo;
    }
	
}
